#include "CSunshine.h"
#include "CRender.h"
#include "CSGD_FModManager.h"
#include "CGamePlayState.h"
#include "testenum.h"

CSunshine::CSunshine(CCloud *pCloud, Vec3 position) : CBase()
{
	m_Pos = position;
	StaticPos = Vec2(position.x, position.y);
	m_vNormal = m_Pos;
	m_vNormal.Normalize();
	CalculateTheta();
	m_bPickedUp = false;
	m_pCloud = pCloud;
	m_fRadius = 1.0f;
	m_unType = PICK_UP;
	m_Quad.MakeShape(Vec2(-2.5f, -2.5f), Vec2(2.5f, 2.5f), 1);
	m_bAlive = true;

	m_fTimer = 0;

	m_pAnimation = CAnimationEngine::GetInstance()->LoadAnimation(Sun_Idle);
	Vec3 NORM(10, 10, 1);
	NORM.Normalize();			//THIS IS NO REASON
}

CSunshine::~CSunshine()
{

}

void CSunshine::Update(float fElapsed)
{
	
	if(!m_bPickedUp)
	{
		m_fTimer += fElapsed * 3;
		/*static Vec3 originalPos = m_Pos;

 		m_Pos = originalPos + m_vNormal * cos(fElapsed)* 5.0f;*/
	}
	else
	{
		m_fTimer += fElapsed;
		CalculateTheta();
		Vec3 vSunShineToCloud = m_pCloud->m_Pos - m_Pos;
		vSunShineToCloud.Normalize();
		m_Vel += vSunShineToCloud * m_fTimer;
		m_Pos += m_Vel*fElapsed;

		CheckCollision(m_pCloud);
	}
}

void CSunshine::Render(void)
{
	if (!m_bPickedUp)
	{
		Vec2 Up = Vec2(-StaticPos.x, -StaticPos.y);
		Up.Normalize();
		Up *= sin(m_fTimer) * 0.75f;
		m_Pos = Vec3(StaticPos + Up, 0);
		CRender::GetInstance()->DrawTex(m_pAnimation->GetCurrentTextureHandle(), &m_Quad, StaticPos + Up, 1,m_fTheta, false);
	}
	else
	{
		CRender::GetInstance()->DrawTex(m_pAnimation->GetCurrentTextureHandle(), &m_Quad, Vec2(m_Pos.x, m_Pos.y), 1,m_fTheta, false);
	}
}

bool CSunshine::CheckCollision(CBase *pBase)
{
	if(m_bPickedUp)
	{
		if(pBase->m_unType == CLOUD)
		{
			Vec3 vSunShineToCloud = pBase->m_Pos - m_Pos;
			if(vSunShineToCloud.LengthSq() < (m_pCloud->m_fRadius + m_fRadius) * (m_pCloud->m_fRadius + m_fRadius))
			{
				// TODO FOR MIKE: This shit collided... do something about it. The cloud ate the sunshine. I swear if you call normalize...
				m_bAlive = false;	
#if LOADSFX
// play the jump sound as long as its not already playing
CSGD_FModManager::GetInstance()->StopSound( CGamePlayState::GetInstance()->m_nSFX[ CGamePlayState::GetInstance()->ITEM_SFX ] );
CSGD_FModManager::GetInstance()->PlaySound( CGamePlayState::GetInstance()->m_nSFX[ CGamePlayState::GetInstance()->ITEM_SFX ] );
CSGD_FModManager::GetInstance()->SetVolume( CGamePlayState::GetInstance()->m_nSFX[ CGamePlayState::GetInstance()->ITEM_SFX ], 0.6f );
#endif LOADSFX
				m_pCloud->m_unCurrState = EAT;
				//m_pCloud->m_vAnimations[m_pCloud->m_unCurrState]->m_fCurrTime = 0;
				m_pCloud->m_vAnimations[m_pCloud->m_unCurrState]->m_unCurrFrame = 0;

				return true;
			}
		}
	}

	return false;
}